//菜单树的配置项
var setting={
    view: {
        // selectedMulti: false, //是否选择多个节点 默认true
        showLine:true   //是否展示连线
    },
    check:{
        enable:true,//是否显示checkbox
        chkStyle: "checkbox",
        chkboxType: { "Y": "p", "N": "s" },
        // checkboxType:{p:"",s:""},//表示父子节点的联动效果
    },
    data: {
        keep: {
            parent:true,   //父节点
            leaf:true      //子节点是否锁定
        },
        simpleData: {
            enable: true,
            idKey : "id", //id
            pIdKey : "pid", //父节点的id :pid的名称 ,默认 Pid
            rootPId : "0" //根节点为空表示最高节点
        }
    },
    callback:{
        onCheck:userCheck,
    }
};

function userCheck(e, treeId, treeNode) {
    treeObj = $.fn.zTree.getZTreeObj("treeUserList");
    var nodes2 = treeObj.getCheckedNodes(true), v = "",ids = "";//获取所有选中的节点
    for(var i = 0 ; i < nodes2.length ; i++) {
        //如果当前所有节点的id!=当前节点id
        if (nodes2[i].id != treeNode.id) {
            nodes2[i].checked = false; //取消选中效果
            treeObj.updateNode(nodes2[i]);//更新所有选中的节点..
        }
    }

}

var addUserOrg={
    userOrgTreeInit:function () {
        //获取树参数
        $.ajax({
            url:application.contextPath+"/user/queryUserOrgByUserId",
            type: 'POST',
            async: false,
            dataType: 'json',
            data:{userId:userId},
            success: function (data) {
                if(!data.isSuccess){
                    layer.msg("加载失败queryOrgTree"+data.message,{icon:5});
                }else {
                    var orgId = new Array();
                    orgId=sessionStorage.getItem("orgId");
                    zNodes = data.data;//加载成功时添加数据
                    if(null !=orgId && "" != orgId){
                        orgId=orgId.split(",");
                        for(var i=0;i<zNodes.length;i++){
                            if($.inArray(zNodes[i].id,orgId)!=-1){
                                zNodes[i].checked=true;
                            }
                        }
                    }
                    $.fn.zTree.init($("#treeUserList"), setting, zNodes);
                }
            },
            error: function (data) {
                layer.msg("网络异常，请稍后再试！");
            }
        });
    },
    addCommitUserOrg:function () {
        var treeObj = $.fn.zTree.getZTreeObj("treeUserList");
        //获取到已经勾选上的节点
        var childNodes=treeObj.getCheckedNodes(true);
        var orgName=new Array();
        var orgId = new Array();
        for(i = 0; i < childNodes.length; i++) {
            orgId[i] = childNodes[i].id;
        }
        parent.$("#orgId").val(orgId);
        var newId=0;
        var n=-1;
        if(childNodes.length>1){//长度大于1
            for(i = 1; i < childNodes.length; i++) {
                if(childNodes[i].level>=newId){
                    if(childNodes[i].level==newId){
                        n=parseInt(n)+1
                        orgName[parseInt(n)] = childNodes[i].name;
                    }else{
                        if(i==childNodes.length-1){
                            n=parseInt(n)+1;
                            orgName[parseInt(n)] = childNodes[i].name;
                        }
                        newId=childNodes[i].level;
                    }
                }else {//当前层级遍历结束
                    newId=childNodes[i].level;
                    if(i==childNodes.length-1){
                        n=parseInt(n)+1;
                        orgName[parseInt(n)] = childNodes[i].name;
                    }
                    n=parseInt(n)+1;
                    orgName[parseInt(n)] = childNodes[i-1].name;
                }
            }
        }else if(childNodes.length=1){
            orgName[0] = childNodes[0].name;
        }
        sessionStorage.setItem("orgId",orgId);
        sessionStorage.setItem("orgName",orgName);
        parent.$("#orgName").val(orgName);
        layer.close(layer.index);
        return false;
    }

};

function getSelectNode() {
    var rootNode = $.fn.zTree.getZTreeObj("treeUserList").getNodesByFilter(function (node) { return node.level == 0 }, true);
    var contrainer = []
    recu(contrainer,rootNode);
    return contrainer;
}


function recu(contrainer,node) {
    if ((node.check_Child_State===2 || node.check_Child_State === -1) &&node.checked)  {
        contrainer.push(node);
        return;
    }
    if (node.check_Child_State===1 &&node.checked){
        var child = node.children;
        if (child.length>0){
            for (var item of child){
                recu(contrainer,item);
            }
        }
    }
}

$(document).ready(function(){
    addUserOrg.userOrgTreeInit();
});